package logic

import (
	"context"
	"person/models/mysql"

	"person/perrpc/internal/svc"
	"person/perrpc/perrpc"

	"github.com/zeromicro/go-zero/core/logx"
	"gorm.io/gorm"
)

type UpdatesLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUpdatesLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdatesLogic {
	return &UpdatesLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UpdatesLogic) Updates(in *perrpc.UpdatesRequest) (*perrpc.UpdatesResponse, error) {
	// todo: add your logic here and delete this line

	user := mysql.User{
		Model:    gorm.Model{},
		Username: in.Username,
		Sex:      in.Sex,
	}

	if err := user.Update(mysql.DB, in.UserId); err != nil {
		return nil, err
	}
	return &perrpc.UpdatesResponse{
		UserId: int64(user.ID),
	}, nil
}
